package com.aistudy.service.impl;

import com.aistudy.entity.User;
import com.aistudy.mapper.UserMapper;
import com.aistudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户服务实现
 *
 * @author AI教学平台
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean register(User user) {
        // 校验用户名和邮箱是否已存在
        if (checkUsernameExists(user.getUsername())) {
            throw new RuntimeException("用户名已存在");
        }
        if (checkEmailExists(user.getEmail())) {
            throw new RuntimeException("邮箱已存在");
        }

        // 加密密码
        user.setPassword(user.getPassword());
        
        // 设置默认值
        user.setRole("STUDENT");
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setEmailNotification(false);
        user.setSmsNotification(false);
        user.setTheme("light");
        user.setIsDeleted(false);
        
        // 保存用户
        return userMapper.insert(user) > 0;
    }

    @Override
    public User login(String username, String password) {
        // 根据用户名查询用户
        User user = findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }

        // 校验密码
        if (password.equals(user.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }

        // 不返回密码
        user.setPassword(null);
        return user;
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public boolean checkUsernameExists(String username) {
        return userMapper.checkUsernameExists(username) > 0;
    }

    @Override
    public boolean checkEmailExists(String email) {
        return userMapper.checkEmailExists(email) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUserInfo(User user) {
        // 不允许修改用户名、密码和邮箱
        user.setUsername(null);
        user.setPassword(null);
        user.setEmail(null);
        
        // 设置更新时间
        user.setUpdateTime(LocalDateTime.now());
        
        return userMapper.updateById(user) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePassword(Long userId, String oldPassword, String newPassword) {
        // 查询用户
        User user = userMapper.getById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 校验旧密码
        if (oldPassword.equals(user.getPassword())) {
            throw new RuntimeException("旧密码错误");
        }
        
        // 更新密码
        User updateUser = new User();
        updateUser.setId(userId);
        updateUser.setPassword(newPassword);
        updateUser.setUpdateTime(LocalDateTime.now());
        
        return userMapper.updateById(updateUser) > 0;
    }
} 